﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using TakeoutMangesBLL;
using Model;
using System.Web.UI.HtmlControls;

namespace TakeoutMangesWeb
{
    public partial class Collect : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Usersession"] != null)
            {
                logininfo.Attributes.Add("Style", "display:none");
                showinfo.Attributes.Add("Style", "display:block");
                Models.Usersession us = Session["Usersession"] as Models.Usersession;
                user.InnerHtml = us.UserName;
            }
            else
            {
                logininfo.Attributes.Add("Style", "display:block");
                showinfo.Attributes.Add("Style", "display:none");
            }
            if (!IsPostBack)
            {
                GetCollect();
            }
        }
        public void GetCollect()
        {
            Models.Usersession u = Session["Usersession"] as Models.Usersession;
            int id = Convert.ToInt32(u.UserID);
            RepeaterCollect.DataSource = ShopcarBLL.GetCollect(id);
            RepeaterCollect.DataBind();
        }

        protected void RepeaterCar_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                Model.Shopcar s = new Model.Shopcar();
                Models.Usersession u = Session["Usersession"] as Models.Usersession;
                s.UserID = u.UserID;
                s.ProductID = Convert.ToInt32(e.CommandArgument.ToString());
                int r = ShopcarBLL.deletecarshow(s);
                if (r > 0)
                {
                    GetCollect();
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "", "<Script>alert('发生错误，请重试')</Script>");
                }
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Session["Usersession"] = null;
            Session.Remove("Usersession");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Login.aspx");
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            List<int> listid = new List<int>();
            List<int> listprice = new List<int>();
            for (int i = 0; i < RepeaterCollect.Items.Count; i++)
            {
                HtmlInputCheckBox check = (HtmlInputCheckBox)RepeaterCollect.Items[i].FindControl("CheckBox1");
                HiddenField hidden = (HiddenField)RepeaterCollect.Items[i].FindControl("HiddenField1");
                HiddenField price = (HiddenField)RepeaterCollect.Items[i].FindControl("HiddenField2");
                Model.Orders OS = new Model.Orders();
                if (check.Checked)
                {
                    listid.Add(Convert.ToInt32(hidden.Value));
                    int ProductID = (Convert.ToInt32(hidden.Value));
                    int r = ShopcarBLL.UpdateCollect(ProductID);
                    if (r > 0)
                    {
                        Response.Redirect("ShopCar.aspx");
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(GetType(), "", "<Script>alert('请勾选想要购买的商品后再付款。')</Script>");
                    }
                }
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("ShopCar.aspx");
        }
    }
}